/*
 * CourseFrame.java
 *
 * Created on 3.5.2010, 11:48:01
 */
package pv1682010.gui;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import pv1682010.Course;
import pv1682010.CourseManager;
import pv1682010.Database;
import pv1682010.User;
import pv1682010.gui.EnrollmentDialog;

/**
 *
 * @author Petr
 */
public class CourseFrame extends javax.swing.JFrame {

    private EventDialog ed = null;
    private AttendanceDialog ad = null;
    private ResourceBundle translations;
    private final DataSource ds;

    /** Creates new form CourseFrame */
    public CourseFrame() {
	ds = new Database();
        new StartDialog(this, true).setVisible(true);
        translations = Translations.getRb();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        courseAddDialog = new javax.swing.JDialog();
        teacherCancelButton = new javax.swing.JButton();
        teacherOkButton = new javax.swing.JButton();
        teacherNameField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        teacherDescriptionArea = new javax.swing.JTextArea();
        jScrollPane6 = new javax.swing.JScrollPane();
        teacherTable = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableCourses = new javax.swing.JTable();
        courseAddButton = new javax.swing.JButton();
        courseDeleteButton = new javax.swing.JButton();
        attendanceButton = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();

        courseAddDialog.setModal(true);

        teacherCancelButton.setText(translations.getString("cancel"));
        teacherCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teacherCancelButtonActionPerformed(evt);
            }
        });

        teacherOkButton.setText("OK");
        teacherOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teacherOkButtonActionPerformed(evt);
            }
        });

        jLabel1.setText(translations.getString("course_name"));

        jLabel2.setText(translations.getString("course_description"));

        teacherDescriptionArea.setColumns(20);
        teacherDescriptionArea.setRows(5);
        jScrollPane5.setViewportView(teacherDescriptionArea);

        teacherTable.setModel(new TeachersTableModel());
        jScrollPane6.setViewportView(teacherTable);

        jLabel3.setText(translations.getString("course_teachers"));

        javax.swing.GroupLayout courseAddDialogLayout = new javax.swing.GroupLayout(courseAddDialog.getContentPane());
        courseAddDialog.getContentPane().setLayout(courseAddDialogLayout);
        courseAddDialogLayout.setHorizontalGroup(
            courseAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(courseAddDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(courseAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(courseAddDialogLayout.createSequentialGroup()
                        .addComponent(teacherOkButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(teacherCancelButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, courseAddDialogLayout.createSequentialGroup()
                        .addGroup(courseAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, courseAddDialogLayout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, courseAddDialogLayout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(teacherNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE))
                        .addGap(442, 442, 442)))
                .addContainerGap())
        );
        courseAddDialogLayout.setVerticalGroup(
            courseAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(courseAddDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(courseAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(teacherNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(courseAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(courseAddDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(teacherOkButton)
                    .addComponent(teacherCancelButton))
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tableCourses.setModel(new CoursesTableModel());
        jScrollPane1.setViewportView(tableCourses);

        courseAddButton.setText(translations.getString("course_add_title"));
        courseAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                courseAddButtonActionPerformed(evt);
            }
        });

        courseDeleteButton.setText(translations.getString("course_delete"));
        courseDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                courseDeleteButtonActionPerformed(evt);
            }
        });

        attendanceButton.setText(translations.getString("attendance"));
        attendanceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                attendanceButtonActionPerformed(evt);
            }
        });

        jButton4.setText(translations.getString("edit_events"));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton7.setText(translations.getString("enroll"));
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jMenu1.setText(translations.getString("file"));

        jMenuItem3.setText(translations.getString("exit"));
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText(translations.getString("edit"));

        jMenuItem2.setText(translations.getString("course_add_title"));
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem1.setText(translations.getString("course_delete"));
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        jMenu3.setText(translations.getString("view"));

        jMenuItem4.setText(translations.getString("user_list"));
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(attendanceButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 244, Short.MAX_VALUE)
                .addComponent(courseAddButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(courseDeleteButton)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 787, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 491, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(courseDeleteButton)
                    .addComponent(attendanceButton)
                    .addComponent(jButton4)
                    .addComponent(jButton7)
                    .addComponent(courseAddButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void attendanceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_attendanceButtonActionPerformed
        try {
            if (tableCourses.getSelectedRowCount() != 1) {
                throw new IllegalArgumentException();
            }
            ad = new AttendanceDialog(this, true);
            ad.setCourseNumber(Integer.parseInt(tableCourses.getValueAt(tableCourses.getSelectedRow(), 0).toString()));
            ad.setVisible(true);
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, translations.getString("sel_one"), translations.getString("not_sel"), 1);
        }
    }//GEN-LAST:event_attendanceButtonActionPerformed

    private void courseAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_courseAddButtonActionPerformed
        addCourse();
    }//GEN-LAST:event_courseAddButtonActionPerformed

    private void teacherCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teacherCancelButtonActionPerformed
        resetTeachers();
    }//GEN-LAST:event_teacherCancelButtonActionPerformed

    private void teacherOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teacherOkButtonActionPerformed
        try {
            CoursesTableModel coursesModel = (CoursesTableModel) tableCourses.getModel();
            Course course = new Course();
            if ("".equals(teacherNameField.getText())) {
                throw new IllegalArgumentException();
            }
            course.setName(teacherNameField.getText());
            course.setDescription(teacherDescriptionArea.getText());
            Collection<User> teachers = new HashSet<User>();
            for (int i : teacherTable.getSelectedRows()) {
                User user = new User();
                user.setUco(Integer.parseInt(teacherTable.getValueAt(i, 0).toString()));
                user.setName(teacherTable.getValueAt(i, 1).toString());
                teachers.add(user);
            }
            course.setTeachers(teachers);
            coursesModel.addCourse(course);
            resetTeachers();
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, translations.getString("fill_name"), null, 2);
        }
    }//GEN-LAST:event_teacherOkButtonActionPerformed

    private void courseDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_courseDeleteButtonActionPerformed
        deleteCourses();
    }//GEN-LAST:event_courseDeleteButtonActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        addCourse();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        deleteCourses();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        setVisible(false);
	this.dispose();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        try {
            if (tableCourses.getSelectedRowCount() != 1) {
                throw new IllegalArgumentException();
            }
            ed = new EventDialog(this, true);
            ed.setCourseNumber(Integer.parseInt(tableCourses.getValueAt(tableCourses.getSelectedRow(), 0).toString()));
            ed.setVisible(true);
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, translations.getString("sel_one"), translations.getString("not_sel"), 1);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
	new UserManagementDialog(this,ds).setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
	if (tableCourses.getSelectedRowCount() > 0) {
	    try {
		int courseId = Integer.parseInt(tableCourses.getValueAt(tableCourses.getSelectedRow(), 0).toString());
		Course course = new CourseManager(ds).getCourseById(courseId);
		new EnrollmentDialog(this, ds, course).setVisible(true);
	    } catch (SQLException ex) {
		Logger.getLogger(CourseFrame.class.getName()).log(Level.SEVERE, null, ex);
	    }
	}
    }//GEN-LAST:event_jButton7ActionPerformed

    private void resetTeachers() {
        teacherDescriptionArea.setText("");
        teacherNameField.setText("");
        teacherTable.clearSelection();
        courseAddDialog.setVisible(false);
    }

    private void deleteCourses() {
        CoursesTableModel coursesModel = (CoursesTableModel) tableCourses.getModel();
        Set<Course> courses = new HashSet<Course>();
        for (int i : tableCourses.getSelectedRows()) {
            Course course = new Course();
            course.setId(Integer.parseInt(tableCourses.getValueAt(i, 0).toString()));
            course.setName(tableCourses.getValueAt(i, 1).toString());
            courses.add(course);
        }
        for (Course course : courses) {
            coursesModel.deleteCourse(course);
        }
    }

    private void addCourse() {
        courseAddDialog.setSize(370, 460);
        courseAddDialog.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CourseFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton attendanceButton;
    private javax.swing.JButton courseAddButton;
    private javax.swing.JDialog courseAddDialog;
    private javax.swing.JButton courseDeleteButton;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTable tableCourses;
    private javax.swing.JButton teacherCancelButton;
    private javax.swing.JTextArea teacherDescriptionArea;
    private javax.swing.JTextField teacherNameField;
    private javax.swing.JButton teacherOkButton;
    private javax.swing.JTable teacherTable;
    // End of variables declaration//GEN-END:variables
}
